package com.aacar.controller.personal;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.aacar.constant.ConsFiled;
import com.aacar.dto.Header;
import com.aacar.dto.MyCarDto;
import com.aacar.interceptor.MemLoginKeyValid;
import com.aacar.service.PersonalCarService;
import com.aacar.utils.ErrorCodeConsField;
import com.aacar.utils.ErrorInfoUtil;
import com.aacar.utils.Pphconfig;
import com.aacar.utils.ResultInfoUtil;
import com.aacar.vo.CommonVo;
import com.aacar.vo.MyCarVo;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;

/**
 * 个人车辆管理
 * @author wangh
 *
 */
@RestController
@RequestMapping("/personalcar")
public class PersonalCarController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private PersonalCarService personalCarService;
	
	/**
	 * 获取个人车辆组合
	 * @return
	 */
	@RequestMapping(value = "/getPersonalCarList/0", method = RequestMethod.POST)
    @ResponseBody
    @MemLoginKeyValid
	public Object getPersonalCarList(HttpServletRequest req) {
		logger.info("request:/getPersonalCarList/0");
		String header = req.getHeader(ConsFiled.Request_Header);//获取请求头
		List<MyCarVo> myCarVo = null;
		Header hd = null;
    	if(null != header) {
    		hd = JSON.parseObject(header, Header.class);
    		myCarVo = this.personalCarService.getPersonalCar(hd.getUserId());
    	}
    	Map<String,Object> resultMap = Maps.newConcurrentMap();
    	resultMap.put("info", myCarVo);
		return ResultInfoUtil.setSuccessInfo(resultMap,hd.getService());
	}
	
	//操作个人车辆信息
	@RequestMapping(value = "/managePersonalCar/0", method = RequestMethod.POST)
    @ResponseBody
    @MemLoginKeyValid
	public Object managePersonalCar(HttpServletRequest req,@RequestBody MyCarDto myCarDto) {
		String header = req.getHeader(ConsFiled.Request_Header);//获取请求头
		String car_flag = myCarDto.getOperatorFlag();
		Header hd = null;
    	if(null != header) {
    		hd = JSON.parseObject(header, Header.class);
    	}
    	CommonVo cvo = null;
    	myCarDto.setUserId(hd.getUserId());//设置用户id
    	if(ConsFiled.Wash_flag_1.equals(car_flag)) {
    		cvo = this.personalCarService.addPersonalCarRecord(myCarDto);
    		if(!cvo.isSuccess()) {
    			return ErrorInfoUtil.setErrorInfo(ErrorCodeConsField.ERROR_MSG_20113, Pphconfig.getContextProperty(ErrorCodeConsField.ERROR_MSG_20113),hd.getService());
    		}
    	}else if(ConsFiled.Wash_flag_2.equals(car_flag)) {
    		cvo = this.personalCarService.updatePersonalCarRecord(myCarDto);
    	}else if(ConsFiled.Wash_flag_3.equals(car_flag)) {
    		//is_valid 设置为0 无效
    		cvo = this.personalCarService.updatePersonalCarRecord(myCarDto);
    	}
    	return ResultInfoUtil.setSuccessInfo(cvo,hd.getService());
	}

}
